/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package br.com.menosehmais.framework.security.cryptography;

import br.com.menosehmais.framework.util.StringUtil;
import java.nio.charset.Charset;
import java.security.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import sun.security.rsa.RSAPublicKeyImpl;

/**
 *
 * @author RafaelRosa
 * @see http://www.java2s.com/Tutorial/Java/0490__Security/BasicRSAexample.htm
 */
public class RSA {
    public static class StringKeyPair {
        private final String publicKey;
        private final PrivateKey privateKey;
        
        public StringKeyPair(String publicKey, PrivateKey privateKey) {
            this.publicKey = publicKey;
            this.privateKey = privateKey;
        }
        
        public String getPublicKey(){
            return publicKey;
        }
        
        public PrivateKey getPrivateKey(){
            return privateKey;
        }
    }
    
    private RSA(){
    }
    
    public static StringKeyPair generateStringKeyPair(){
        StringKeyPair stringKeyPair = null;
        KeyPair keyPair = generateKeyPair();
        stringKeyPair = toStringKeyPair(keyPair);
        return stringKeyPair;
    }
    
    public static StringKeyPair toStringKeyPair(KeyPair keyPair){
        StringKeyPair stringKeyPair = null;
        String publicKey = StringUtil.toHexaString(keyPair.getPublic().getEncoded());
        //String privateKey = toHexaString(keyPair.getPrivate().getEncoded());
        stringKeyPair  = new StringKeyPair(publicKey, keyPair.getPrivate());
        return stringKeyPair;
    }
    
    public static KeyPair generateKeyPair() {
        KeyPairGenerator keyGen;
        try {
            keyGen = KeyPairGenerator.getInstance("RSA");
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(RSA.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
        keyGen.initialize(1024);
        KeyPair keypair = keyGen.genKeyPair();
        
        return keypair;
    }
    
    public static KeyPair toKeyPair(StringKeyPair stringKeyPair) throws InvalidKeyException{
        PublicKey publicKey = new RSAPublicKeyImpl(stringKeyPair.getPublicKey().getBytes(Charset.forName("UTF-8")));
        //PrivateKey privateKey = new RSAPrivateKeyImpl(stringKeyPair.getPrivateKey().getBytes(Charset.forName("UTF-8")));
        KeyPair keyPair = new KeyPair(publicKey, stringKeyPair.getPrivateKey());
        return keyPair;
    }
    
    public static String assign(PrivateKey privateKey, String message) {
        return null;//TODO
    }
    
    public static String desassign(PublicKey publicKey, String assignedMessage) {
        return null;//TODO
    }
    
    public static String encrypt(PublicKey publicKey, String message) {
        return null;//TODO
    }
    
    public static String decrypt(PrivateKey privateKey, String encrypetedMessage) {
        return null;//TODO
    }
}
